/** TPC Technologies Inc. 15 octobre 2011**/

var request = false;

function submitOnEnter() {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    }

    if (keycode == 13) {
        validateLogin();
    }
}

function checkboxChecker(elementId) {
    if (document.getElementById(elementId).checked == true) {
        document.getElementById(elementId).checked = false;
    }
    else {
        document.getElementById(elementId).checked = true;
    }
}

function validateLogin() {
	document.getElementById('loginLoader').src = 'images/loginLoading.gif';
    if (document.getElementById('clickedOn').value == 'no') {
        if ((document.getElementById("tpc_username").value != null) && (document.getElementById("tpc_password").value != null)) {
            if ((document.getElementById("tpc_username").value.length > 1) && (document.getElementById("tpc_password").value.length > 1)) {
                document.getElementById('clickedOn').value='yes';
                document.getElementById('tpc_username').disabled=true;
                document.getElementById('tpc_password').disabled=true;
                setAjaxRequest();
                if (request) {
                    var username = document.getElementById("tpc_username").value;
                    username = encodeURI(username);
                    var password = document.getElementById("tpc_password").value;
                    password = encodeURIComponent(password);
                    request.open("GET","checkLogin.jsp?tpcusername="+username+"&tpcpassword="+password,true);
                    request.onreadystatechange = function() {acceptLogin();};
                    request.send(null);
                }
            }
            else {
                accessDenied("SVP entrez un nom d'usager et un mot de passe.");
                document.getElementById('loginLoader').src = 'images/spacer_login.png';
            }
        }
        else {
            accessDenied("SVP entrez un nom d'usager et un mot de passe.");
            document.getElementById('loginLoader').src = 'images/spacer_login.png';
        }
    }
}

function acceptLogin() {
    if (request.readyState == 4) {
        if (request.status == 200) {
            var serverResponse = request.responseText;
            if (serverResponse.indexOf("denied") == -1) {
                /** SET COOKIES **/
                window.location = serverResponse;
            }
            else {
                accessDenied('Acces Interdit. Essayez de nouveau.');
                document.getElementById('loginLoader').src = 'images/spacer_login.png';
            }
        }
        else {
            accessDenied('Acces Interdit. Essayez de nouveau.');
            document.getElementById('loginLoader').src = 'images/spacer_login.png';
        }
    }
}

function accessDenied(message) {
    document.getElementById('tpc_warning').innerHTML='<table><tr><td><img src="images/accessdenied.png" height="0" width="0" id="tpc_imageWarning"></td><td class="style_Text_Login_Warning">'+message+'</td></tr></table>';
    document.getElementById('tpc_warning').height = "32px";
    document.getElementById('tpc_imageWarning').width = 32;
    document.getElementById('tpc_imageWarning').height = 32;
    document.getElementById('clickedOn').value='no';
    document.getElementById('tpc_username').disabled=false;
    document.getElementById('tpc_username').value='';
    document.getElementById('tpc_password').disabled=false;
    document.getElementById('tpc_password').value='';
    document.getElementById('tpc_username').focus();
}

function setAjaxRequest() {

    try {
        request = new XMLHttpRequest();
    }
    catch (trymicrosoft) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed) {
                request = false;
            }
        }
    }
}